using Azure;
using Crossborder.Common.Utility.HttpHelper;
using Crossborder.Core.Dto;
using Crossborder.Domian.Products;

namespace Crossborder.Service.Products
{
    public partial class ProductService
    {
        public void GetCategoryInfo(string categoryId)
        {

        }

        public async Task<HttpResponsion> QueryCategory(CategoryDto dto)
        {
            var response = new HttpResponsion();
            var querySql = "select Id,CategoryId,CategoryName,ParentId,Level,CreateDate,ModifyDate,IsEnabled,IsDelete from Categorys where 1=1";
            var result = await _CatRep.QueryAsync(querySql, dto);
            if (result != null)
            {
                var data = _Map.Map<List<CategoryDto>>(result);
                response = HttpHelper.Success(data);
            }
            else
            {
                response = HttpHelper.Fail("数据查询失败");
            }
            return response;
        }

        public async Task<HttpResponsion> QueryCategoryPage(int page, int size, CategoryDto dto)
        {
            var responsion = new HttpResponsion();
            var querySql = "select Id,CategoryId,CategoryName,ParentId,Level,CreateDate,ModifyDate,IsEnabled,IsDelete from Categorys where 1=1";
            var result = await _CatRep.QueryPageAsync(page, size, querySql, dto, true);
            if (result != null)
            {
                var data = _Map.Map<List<CategoryDto>>(result);
                responsion = HttpHelper.Success(data);
            }
            else
            {
                responsion = HttpHelper.Fail("数据查询失败");
            }
            return responsion;
        }

        public async Task<HttpResponsion> CreateCategory(CategoryInputDto dto)
        {
            var category = _Map.Map<Categorys>(dto);
            category.Create();
            if (string.IsNullOrWhiteSpace(category.ParentId))
            {
                category.ParentId = "0";
            }
            if (category.Level == null)
            {
                category.Level = 1;
            }

            var result = await _CatRep.InsertAsync(category);
            if (result == null)
            {
                
                return HttpHelper.Fail("创建失败！");
            }
            var data = _Map.Map<CategoryDto>(result);
            return HttpHelper.Success(data);;
        }

        private async Task<CategoryDto> GetCategoryByCategoryId(string categoryId)
        {
            var category = await _CatRep.GetAsync(m => m.CategoryId == categoryId);
            return _Map.Map<CategoryDto>(category);
        }

        // public async Task<HttpResponsion> SetCategoryMemory(string userId, CategoryInputDto dto)
        // {
        //     var response = new HttpResponsion();
        //     var category = _Map.Map<Categorys>(dto);
        //     category.Create();
        //     string key = "crat:" + userId;

        //     await _RedisWorker.SetHashMemory(key, category.CategoryId, dto);
        //     return response;
        // }

        // public async Task<HttpResponsion> GetHashAllMemory(string key)
        // {
        //     var response = new HttpResponsion();
        //     await _RedisWorker.GetHashAllMemory(key);
        //     return response;
        // }
    }
}